<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);

//定义对象用来保存围栏搜索条件
const geofenceSearchForm = ref({
  name: '',
  status: ''
})

//定义数组用来保存围栏表格数据
const geoTableData = ref([])

//定义加载围栏方法
const loadGeo = () => {
  let data = qs.stringify(geofenceSearchForm.value);
  axios.get(BASE_URL + '/v1/geofence/select?' + data)
      .then(response => {
        if (response.data.code == 2000) {
          geoTableData.value = response.data.data
          ElMessage.success("查询成功")
          // geofenceSearchForm.value={name:'', status:''}
        } else {
          ElMessage.error(response.data.msg)
        }
      })
}

onMounted(() => {
  loadGeo();
})

//重置搜索表单
const resetSearchForm = () => {
  geofenceSearchForm.value = {name: '', status: ''}
  loadGeo();
}

//定义一个全局变量存储围栏id
var geoId;

//查询已绑定车辆
const loadVehicle=(id)=>{
  bindVehicleDialogVisible.value=true;
  geoId=id;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceId='+geoId)
      .then(response => {
        if (response.data.code == 2000) {
          bindVehicleArr.value=response.data.data
        } else {
          ElMessage.error(response.data.msg)
        }
      })
}

const bindVehicleArr=ref([])

//解除车辆绑定
const removeBindVehicle=(row)=>{
  axios.post(BASE_URL + '/v1/vehicle/unbind?id='+row.id)
      .then(response=>{
        if (response.data.code == 2000){
          ElMessage.success("解绑成功")
          loadVehicle(geoId);
        }
      })
}

const unBindVehicleArr=ref([])

//查询未绑定车辆列表
const showUnbindVehicleList=()=>{
  AddBindVehicleDialogVisible.value=true;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceBindStatus=0')
      .then(response=>{
        if (response.data.code == 2000){
          unBindVehicleArr.value=response.data.data
          ElMessage.success("查询成功")
        }else{
          ElMessage.error(response.data.msg)
        }
      })
}

//定义给围栏绑定车辆的方法，注意：此处的id是车辆id
const bindNewVehicle=(id)=>{
  let da={id:id,geofenceId:geoId}
  let data=qs.stringify(da)
  axios.post(BASE_URL + '/v1/vehicle/bind',data)
      .then(response=>{
        if (response.data.code == 2000){
          ElMessage.success("绑定成功")
          showUnbindVehicleList();//每绑定一辆车辆，就需要重新加载所有可绑定车辆
        }else{
          ElMessage.error(response.data.msg)
        }
      })
}

//处理内层弹窗关闭的方法
const offDialog=()=>{
    loadVehicle(geoId);
    AddBindVehicleDialogVisible.value=false;
}

// const removeGeofence=(id)=>{
//   console.log("id:"+id)
//   axios.get(BASE_URL + '/v1/vehicle/select?geofenceId='+id)
//       .then(response => {
//         if (response.data.code == 2000) {
//           bindVehicleArr.value=response.data.data
//           if(bindVehicleArr.value.length>0){
//             bindVehicleDialogVisible.value=true;
//             ElMessage.error("请先解除所有车辆绑定")
//           }
//           else{
//             console.log("删除围栏成功")
//             axios.post(BASE_URL + '/v1/geofence/delGeofence/'+id)
//                 .then(response=>{
//                   if (response.data.code == 2000){
//                     ElMessage.success("删除成功")
//                     loadGeo();
//                   }else{
//                     ElMessage.error(response.data.msg)
//                   }
//                 })
//           }
//         } else {
//           ElMessage.error(response.data.msg)
//         }
//       })
// }

const removeGeofence=(id)=>{
  if(confirm("确定删除该围栏？")){
    axios.post(BASE_URL + '/v1/geofence/delGeofence/'+id)
        .then(response=>{
          if (response.data.code == 2000){
            ElMessage.success("删除成功")
            loadGeo();
          }else{
            ElMessage.error('请先解除绑定车辆')
          }
        })
  }
}

const statusSwitch=(id,status)=>{
  let data=qs.stringify({id:id,status:status})
  axios.post(BASE_URL + '/v1/geofence/save',data)
      .then(response=>{
        if (response.data.code == 2000){
          ElMessage.success("修改成功")
          loadGeo();
        }else{
          ElMessage.error(response.data.msg)
        }
      })
}
</script>

<template>
  <div style="height: 6vh;background-color: #fff;padding: 10px 20px">
    <span style="line-height: 60px;font-size: 20px">围栏管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px" @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <div style="height: 94vh">
    <el-card style="margin: 10px;height: 70px">
      <el-form :inline="true">
        <el-form-item label="围栏名称">
          <el-input placeholder="请输入围栏名称" v-model="geofenceSearchForm.name"
                    style="width: 200px" @keydown.enter="loadGeo"></el-input>
        </el-form-item>
        <el-form-item label="围栏状态">
          <el-select placeholder="请选择围栏状态" v-model="geofenceSearchForm.status"
                     style="width: 200px" @change="loadGeo">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetSearchForm">重置</el-button>
          <el-button type="primary" @click="loadGeo">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin: 10px">
      <el-table :data="geoTableData" style="width: 100%;">
        <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
        <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
        <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
        <el-table-column label="围栏状态" align="center">
          <template #default="scope">
            <el-switch inactive-value="0" active-value="1" v-model="scope.row.status" @change="statusSwitch(scope.row.id,scope.row.status)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
            <el-button type="primary" link @click="removeGeofence(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>

  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="showUnbindVehicleList">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeBindVehicle(scope.row)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="offDialog">
    <el-table :data="unBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bindNewVehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<style scoped>

</style>